package com.dingup.provider.handler;

import com.suyeer.basic.util.BEmojiUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import static com.dingup.provider.util.ConstUtil.PREFIX_OF_EMOJI_ENCODER;

/**
 * Created by jun on 2018/1/31.
 */
public class EmojiHandler extends BaseTypeHandler<String> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, BEmojiUtil.encodeIfContainsEmoji(parameter, PREFIX_OF_EMOJI_ENCODER));
    }

    @Override
    public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return BEmojiUtil.decodeIfContainsPrefix(rs.getString(columnName), PREFIX_OF_EMOJI_ENCODER);
    }

    @Override
    public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return BEmojiUtil.decodeIfContainsPrefix(rs.getString(columnIndex), PREFIX_OF_EMOJI_ENCODER);
    }

    @Override
    public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return BEmojiUtil.decodeIfContainsPrefix(cs.getString(columnIndex), PREFIX_OF_EMOJI_ENCODER);
    }

}